var CronJob = require("cron").CronJob;
var KOC = require("koc.common.utils");
var DALFactory = require("./dal/dal-factory");
var Config = require("../config.js");
var CommonUtils = require("./common-utils");

// # ┌────────────── second (optional)
// # │ ┌──────────── minute
// # │ │ ┌────────── hour
// # │ │ │ ┌──────── day of month
// # │ │ │ │ ┌────── month 0-11
// # │ │ │ │ │ ┌──── day of week 0-6
// # │ │ │ │ │ │
// # │ │ │ │ │ │
// # * * * * * *

//每分钟执行
new CronJob({
    cronTime: "00 * * * * *",
    onTick: function () {
        console.log(KOC.Moment().format("YYYY-MM-DD HH:mm:ss"), "00 * * * * * : 作业开始");
        DALFactory.Business.RedisCommon_UserAnchorList(function (retValue) {
            if (retValue.hasError) {
                console.log("Fail Hot Work Every Minutes");
            } else {
                console.log("Successed Hot Work Every Minutes");
            }
        });
    }
}).start();

//每半点和整点执行
new CronJob({
    cronTime: "00 00,30 * * * *",
    onTick: function () {
        console.log(KOC.Moment().format("YYYY-MM-DD HH:mm:ss"), "00 00,30 00 * * * : 作业开始");
        DALFactory.Business.User_AnchorHotNext(function (retValue) {
            if (retValue.hasError) {
                console.log("ERROR:Business.User_AnchorHotNext:" + retValue);
            } else {
                console.log("SUCCESS:Business.User_AnchorHotNext");
            }
        });
        DALFactory.Business.RedisCommon_GiftUserRankingWeekReset(function (retValue) {
            if (retValue.hasError) {
                console.log("ERROR:Business.RedisCommon_GiftUserRankingWeekReset:" + retValue);
            } else {
                console.log("SUCCESS:Business.RedisCommon_GiftUserRankingWeekReset");
            }
        });
    }
}).start();

//每天00:10分执行
new CronJob({
    cronTime: "00 10 00 * * *",
    onTick: function () {
        console.log(KOC.Moment().format("YYYY-MM-DD HH:mm:ss"), "00 10 00 * * * : 作业开始");
        DALFactory.User.User_Info.Update_Scheduler(function (retValue) {
            if (retValue.hasError) {
                console.log("ERROR:User.User_Info.Update_Scheduler:" + retValue);
            } else {
                console.log("SUCCESS:User.User_Info.Update_Scheduler");
            }
        });
        DALFactory.User.User_Anchor.Update_Scheduler(function (retValue) {
            if (retValue.hasError) {
                console.log("ERROR:User.User_Anchor.Update_Scheduler:" + retValue);
            } else {
                console.log("SUCCESS:User.User_Anchor.Update_Scheduler");
            }
        });
        DALFactory.User.User_Task.Update_Scheduler(function (retValue) {
            if (retValue.hasError) {
                console.log("ERROR:User.User_Task.Update_Scheduler:" + retValue);
            } else {
                console.log("SUCCESS:User.User_Task.Update_Scheduler");
            }
        });
        DALFactory.User.User_Contribution.Update_Scheduler(function (retValue) {
            if (retValue.hasError) {
                console.log("ERROR:User.User_Contribution.Update_Scheduler:" + retValue);
            } else {
                console.log("SUCCESS:User.User_Contribution.Update_Scheduler");
            }
        });
        DALFactory.User.User_Anchor_Blacklist.Update_Scheduler(function (retValue) {
            if (retValue.hasError) {
                console.log("ERROR:User.User_Anchor_Blacklist.Update_Scheduler:" + retValue);
            } else {
                console.log("SUCCESS:User.User_Anchor_Blacklist.Update_Scheduler");
            }
        });
        DALFactory.User.User_Anchor_Blacklist.Delete_Scheduler(function (retValue) {
            if (retValue.hasError) {
                console.log("ERROR:User.User_Anchor_Blacklist.Delete_Scheduler:" + retValue);
            } else {
                console.log("SUCCESS:User.User_Anchor_Blacklist.Delete_Scheduler");
            }
        });
    }
}).start();

//每天00:15分执行
new CronJob({
    cronTime: "00 15 00 * * *",
    onTick: function () {
        console.log(KOC.Moment().format("YYYY-MM-DD HH:mm:ss"), "00 15 00 * * * : 作业开始");
        KOC.Async.series([
            function (callback) {
                DALFactory.Report.Channel_Day.Scheduler(function (retValue) {
                    if (retValue.hasError) {
                        console.log("ERROR:Report.Channel_Day.Scheduler:" + retValue);
                    } else {
                        console.log("SUCCESS:Report.Channel_Day.Scheduler");
                    }
                    callback();
                });
            },
            function (callback) {
                DALFactory.Report.Anchor_Day.Scheduler(function (retValue) {
                    if (retValue.hasError) {
                        console.log("ERROR:Report.Anchor_Day.Scheduler:" + retValue);
                    } else {
                        console.log("SUCCESS:Report.Anchor_Day.Scheduler");
                    }
                    callback();
                });
            },
            function (callback) {
                DALFactory.Report.Platform_Day.Scheduler(function (retValue) {
                    if (retValue.hasError) {
                        console.log("ERROR:Report.Platform_Day.Scheduler:" + retValue);
                    } else {
                        console.log("SUCCESS:Report.Platform_Day.Scheduler");
                    }
                    callback();
                });
            },
            function (callback) {
                DALFactory.Report.Anchor_Family_Month.Scheduler(function (retValue) {
                    if (retValue.hasError) {
                        console.log("ERROR:Report.Anchor_Family_Month.Scheduler:" + retValue);
                    } else {
                        console.log("SUCCESS:Report.Anchor_Family_Month.Scheduler");
                    }
                    callback();
                });
            },
            function (callback) {
                DALFactory.User.User_Anchor.Update_SchedulerForReport(function (retValue) {
                    if (retValue.hasError) {
                        console.log("ERROR:User.User_Anchor.Update_SchedulerForReport:" + retValue);
                    } else {
                        console.log("SUCCESS:User.User_Anchor.Update_SchedulerForReport");
                    }
                    callback();
                });
            },


            function (callback) {
                DALFactory.Report.Channel_Day_.Scheduler(function (retValue) {
                    if (retValue.hasError) {
                        console.log("ERROR:Report.Channel_Day_.Scheduler:" + retValue);
                    } else {
                        console.log("SUCCESS:Report.Channel_Day_.Scheduler");
                    }
                    callback();
                });
            }
        ], function () {
        });
    }
}).start();

//order_pay 每天3点取消前一天没有支付的单据??????

//每天23:55分执行（每日统计）
new CronJob({
// # ┌────────────── second (optional)
// # │ ┌──────────── minute
// # │ │ ┌────────── hour
// # │ │ │ ┌──────── day of month
// # │ │ │ │ ┌────── month
// # │ │ │ │ │ ┌──── day of week
// # │ │ │ │ │ │
// # │ │ │ │ │ │
// # * * * * * *
    cronTime: "00 55 23 * * *",
    onTick: function () {
        console.log(KOC.Moment().format("YYYY-MM-DD HH:mm:ss"), "每日统计作业开始");
        var _params = {
            UV: null,//独立访客个数
            Regist_Day_Number: null,//当天新增注册人数
            Registration_Conversion: null,//注册转化率（新增注册人数/独立访客）
            Login_Day_User: null,//当天登录人数
            Regist_Total_User: null,//累计注册人数
            Actrate: null,//活跃率（登录的注册用户/总注册人数）
            Recharge_Day_User: null,//当天充值人数
            Recharge_Day_Coin: null,//当天充值金额
            Recharge_Total_User: null,//累计充值总人数
            Pay_Day_User: null,//当天消费人数
            Pay_Day_Coin: null,//当天消费金额
            ARPU: null,//ARPU值=当天消费的总金额/消费总人数
            IP_Count: null//外网ip的数量
        };
        KOC.Async.series([
            //不重复外网IP个数（总数）
            function (callback) {
                DALFactory.User.User_Info.Info_IPCount(function (retValue) {
                    if (retValue.hasError) {
                        retValue.message = "User_Info.Info_IPCount:" + retValue;
                        callback(retValue);
                        return;
                    }
                    _params.IP_Count = retValue.returnObject && retValue.returnObject.IP_Count ? retValue.returnObject.IP_Count : 0;
                    callback();
                });
            },
            //当天新增注册人数
            function (callback) {
                DALFactory.User.User_Info.Info_UserCountDay(function (retValue) {
                    if (retValue.hasError) {
                        retValue.message = "User_Info.Info_UserCountDay:" + retValue;
                        callback(retValue);
                        return;
                    }
                    _params.Regist_Day_Number = retValue.returnObject && retValue.returnObject.User_Count ? retValue.returnObject.User_Count : 0;
                    callback();
                });
            },
            //#累计注册人数
            function (callback) {
                DALFactory.User.User_Info.Info_UserCountALL(function (retValue) {
                    if (retValue.hasError) {
                        retValue.message = "User_Info.Info_UserCountALL:" + retValue;
                        callback(retValue);
                        return;
                    }
                    _params.Regist_Total_User = retValue.returnObject && retValue.returnObject.User_Count ? retValue.returnObject.User_Count : 0;
                    callback();
                });
            },
            //#当天登录人数
            function (callback) {
                DALFactory.User.User_Info.Info_UserCountLogin(function (retValue) {
                    if (retValue.hasError) {
                        retValue.message = "User_Info.Info_UserCountLogin:" + retValue;
                        callback(retValue);
                        return;
                    }
                    _params.Login_Day_User = retValue.returnObject && retValue.returnObject.User_Count ? retValue.returnObject.User_Count : 0;
                    callback();
                });
            },
            //#当天充值人数
            function (callback) {
                DALFactory.User.Order_Pay.Info_UserCountDay(function (retValue) {
                    if (retValue.hasError) {
                        retValue.message = "Order_Pay.Info_UserCountDay:" + retValue;
                        callback(retValue);
                        return;
                    }
                    _params.Recharge_Day_User = retValue.returnObject && retValue.returnObject.User_Count ? retValue.returnObject.User_Count : 0;
                    callback();
                });
            },
            //#充值金额（当天）
            function (callback) {
                DALFactory.User.Order_Pay.Info_MoneyDay(function (retValue) {
                    if (retValue.hasError) {
                        retValue.message = "Order_Pay.Info_MoneyDay:" + retValue;
                        callback(retValue);
                        return;
                    }
                    _params.Recharge_Day_Coin = retValue.returnObject && retValue.returnObject.Sum_Money ? retValue.returnObject.Sum_Money : 0;
                    callback();
                });
            },
            //#累计充值总人数
            function (callback) {
                DALFactory.User.Order_Pay.Info_UserCountALL(function (retValue) {
                    if (retValue.hasError) {
                        retValue.message = "Order_Pay.Info_UserCountALL:" + retValue;
                        callback(retValue);
                        return;
                    }
                    _params.Recharge_Total_User = retValue.returnObject && retValue.returnObject.User_Count ? retValue.returnObject.User_Count : 0;
                    callback();
                });
            },
            //#累计充值总金额
            function (callback) {
                DALFactory.User.Order_Pay.Info_MoneyALL(function (retValue) {
                    if (retValue.hasError) {
                        retValue.message = "Order_Pay.Info_MoneyALL:" + retValue;
                        callback(retValue);
                        return;
                    }
                    _params.Recharge_Total_Coin = retValue.returnObject && retValue.returnObject.Sum_Money ? retValue.returnObject.Sum_Money : 0;
                    callback();
                });
            },
            //#守护当天消费总数
            function (callback) {
                DALFactory.User.Order_Guardian.Info_PayMoneyDay(function (retValue) {
                    if (retValue.hasError) {
                        retValue.message = "Order_Guardian.Info_PayMoneyDay:" + retValue;
                        callback(retValue);
                        return;
                    }
                    _params.Pay_Day_Coin += retValue.returnObject && retValue.returnObject.Amt_Count ? retValue.returnObject.Amt_Count : 0;
                    callback();
                });
            },
            //#守护当天消费总人数
            function (callback) {
                DALFactory.User.Order_Guardian.Info_UserCountDay(function (retValue) {
                    if (retValue.hasError) {
                        retValue.message = "Order_Guardian.Info_UserCountDay:" + retValue;
                        callback(retValue);
                        return;
                    }
                    _params.Pay_Day_User += retValue.returnObject && retValue.returnObject.User_Count ? retValue.returnObject.User_Count : 0;
                    callback();
                });
            },
            //#VIP当天消费总数
            function (callback) {
                DALFactory.User.Order_VIP.Info_PayMoneyDay(function (retValue) {
                    if (retValue.hasError) {
                        retValue.message = "Order_VIP.Info_PayMoneyDay:" + retValue;
                        callback(retValue);
                        return;
                    }
                    _params.Pay_Day_Coin += retValue.returnObject && retValue.returnObject.Amt_Count ? retValue.returnObject.Amt_Count : 0;
                    callback();
                });
            },
            //#VIP当天消费总人数
            function (callback) {
                DALFactory.User.Order_VIP.Info_UserCountDay(function (retValue) {
                    if (retValue.hasError) {
                        retValue.message = "Order_VIP.Info_UserCountDay:" + retValue;
                        callback(retValue);
                        return;
                    }
                    _params.Pay_Day_User += retValue.returnObject && retValue.returnObject.User_Count ? retValue.returnObject.User_Count : 0;
                    callback();
                });
            },
            //#礼物当天消费总数
            function (callback) {
                DALFactory.User.Order_Gift.Info_PayMoneyDay(function (retValue) {
                    if (retValue.hasError) {
                        retValue.message = "Order_Gift.Info_PayMoneyDay:" + retValue;
                        callback(retValue);
                        return;
                    }
                    _params.Pay_Day_Coin += retValue.returnObject && retValue.returnObject.Amt_Count ? retValue.returnObject.Amt_Count : 0;
                    callback();
                });
            },
            //#礼物当天消费总人数
            function (callback) {
                DALFactory.User.Order_Gift.Info_UserCountDay(function (retValue) {
                    if (retValue.hasError) {
                        retValue.message = "Order_Gift.Info_UserCountDay:" + retValue;
                        callback(retValue);
                        return;
                    }
                    _params.Pay_Day_User += retValue.returnObject && retValue.returnObject.User_Count ? retValue.returnObject.User_Count : 0;
                    callback();
                });
            },
            //#坐骑当天消费总数
            function (callback) {
                DALFactory.User.Order_Mounts.Info_PayMoneyDay(function (retValue) {
                    if (retValue.hasError) {
                        retValue.message = "Order_Mounts.Info_PayMoneyDay:" + retValue;
                        callback(retValue);
                        return;
                    }
                    _params.Pay_Day_Coin += retValue.returnObject && retValue.returnObject.Amt_Count ? retValue.returnObject.Amt_Count : 0;
                    callback();
                });
            },
            //#坐骑当天消费总人数
            function (callback) {
                DALFactory.User.Order_Mounts.Info_UserCountDay(function (retValue) {
                    if (retValue.hasError) {
                        retValue.message = "Order_Mounts.Info_UserCountDay:" + retValue;
                        callback(retValue);
                        return;
                    }
                    _params.Pay_Day_User += retValue.returnObject && retValue.returnObject.User_Count ? retValue.returnObject.User_Count : 0;
                    callback();
                });
            },
            //插入每日用户统计记录
            function (callback) {
                DALFactory.User.User_Day_Statistics.Insert({
                    UV: 0,
                    Regist_Day_Number: _params.Regist_Day_Number,
                    Registration_Conversion: _params.Registration_Conversion,
                    Login_Day_User: _params.Login_Day_User,
                    Regist_Total_User: _params.Regist_Total_User,
                    Actrate: _params.Login_Day_User / _params.Regist_Total_User,
                    Recharge_Day_User: _params.Recharge_Day_User,
                    Recharge_Day_Coin: _params.Recharge_Day_Coin,
                    Recharge_Total_User: _params.Recharge_Total_User,
                    Pay_Day_User: _params.Pay_Day_User,
                    Pay_Day_Coin: _params.Pay_Day_Coin,
                    ARPU: _params.Pay_Day_Coin / _params.Pay_Day_User,
                    Web_IP_Number: _params.IP_Count
                }, function (retValue) {
                    if (retValue.hasError) {
                        retValue.message = "User_Day_Statistics.Delete_Scheduler:" + retValue;
                        callback(retValue);
                        return;
                    }
                    callback();
                });
            }
        ], function (retValue) {
            if (retValue) {
                console.log(retValue.message);
                return;
            }
            console.log(KOC.Moment().format("YYYY-MM-DD HH:mm:ss"), "每日统计作业成功");
            return;
        });
    }
}).start();

//每周天的00:01分00秒执行(周星榜，徽章发放)
new CronJob({
    cronTime: "00 01 00 * * SUN",
    onTick: function () {
        console.log(KOC.Moment().format("YYYY-MM-DD HH:mm:ss"), "00 01 00 ? * SUN : 作业开始");
        DALFactory.Business.User_WeekStarGrantEmblem(function (retValue) {
            if (retValue.hasError) {
                console.log("徽章发放失败,Time:", new Date(), retValue);
            } else {
                console.log("徽章发放成功,Time:", new Date());
            }
        });
    }
}).start();